#ifndef _CONSTANTS_HPP
#define _CONSTANTS_HPP 1

//tags

#define CLIENT_REQUEST 1
#define RELEASE_CP 3
#define RETRANSMIT 4
#define LOAD_CP_MESSAGE 5
#define LAST_EXECUTED 6
#define CP_TOKEN_MESSAGE 7
#define REPLY 8
#define SIGNED_REQUEST_CORE 9

#define REQUEST_CP 10
#define SPECULATIVE_NEXT_BATCH 11
#define TENTATIVE_NEXT_BATCH 12
#define COMMITTED_NEXT_BATCH 13
#define CP_LOADED 14
#define WATCH_REPLY 15
#define READ_ONLY_REQUEST 16
#define READ_ONLY_REPLY 17
#define FILTERED_REQUEST_CORE 18
#define FILTERED_REQUEST 19

#define BATCH_COMPLETED 20
#define FETCH_COMMAND 21
#define FORWARD_COMMAND 22
#define FETCH_DENIED 23

#define RELAY_OPS 30
#define CP_TOKEN 31
#define MISSING_CP 32
#define RELAY_CP 33 
#define CP_MESSAGE 34
#define ORDER_STATUS 35
#define FORWARDED_REQUEST 36
#define START_VIEW 37

#define PRE_PREPARE 40
#define PREPARE 41
#define COMMIT 42
#define VIEW_CHANGE 43
#define VIEW_CHANGE_ACK 44
#define NEW_VIEW 45
#define CONFIRM_VIEW 46
#define MISSING_VIEW_CHANGE 47
#define RELAY_VIEW_CHANGE 48
#define MISSING_OPS 49


//field sizes expressed in bytes

#define MARKER_SIZE 4
#define TAG_SIZE 2
#define SIZE_SIZE 4
#define SENDER_SIZE 4
#define CLIENT_ID_SIZE 4
#define REQUEST_ID_SIZE 4
#define COMMAND_SIZE_SIZE 4
#define COMMAND_DIGEST_FLAG_SIZE 1
#define NON_DETERMINISM_SIZE 16
#define NON_DETERMINISM_SIZE_SIZE 2
#define CE_COMMAND_BATCH_SIZE_SIZE 2 //for use wile rewding/writing a certificate entry
#define NB_COMMAND_BATCH_SIZE_SIZE 4 //for use when reading/writing a next batch. keeping two different sizes for backward compatibility
#define ENTRY_COUNT_SIZE 2
#define VIEW_NUMBER_SIZE 4
#define SEQUENCE_NUMBER_SIZE 4
#define CHECKPOINT_FLAG_SIZE 2
#define TOKEN_SIZE_SIZE 4 //size of the fields that contains the size of a CP token
#define TIME_SIZE 4
#define ENTRY_BATCH_SIZE_SIZE 2
#define CHECKPOINTSTATE_SIZE_SIZE 4
#define CERTIFICATE_ENTRY_SIZE_SIZE 4
#define OPS_COUNT_SIZE 2

//authentication sizes expressed in bytes

#define DIGEST_SIZE 16
#define MAC_SIZE 20
#define SIGNATURE_SIZE 128


//parameters

#define INSECURE 0
#define FILTER_COUNT 0
#define EXECUTION_COUNT 1
#define ORDER_COUNT 1
#define FILTERED 0

// other magic number

#define QUEUE_SIZE 1024
#define BYTES_SIZE_THRESHOLD 60000 //max size of a message
#define CONF_LINE_LENGTH 1024 //max number of chars in a single configuration line
#define CONFIG_FILE_NAME "config.conf" 	// default configuration file
#define BACKLOG_SIZE 128 //size of the backlog used by listen. 
			 //NB: this is equal to the default value of
			 // /proc/sys/net/core/somaxconn in linux machine
			 // for higher backlogs, it is necessary to ingrease that
			 // kernel parameter also
#define TV_SEC  1
#define TV_USEC 0

#define MAX_QUEUE_SIZE 1024

#include <stdlib.h>

#define	BSWAP_64(x)     (((uint64_t)(x) << 56) | \
                        (((uint64_t)(x) << 40) & 0xff000000000000ULL) | \
                        (((uint64_t)(x) << 24) & 0xff0000000000ULL) | \
                        (((uint64_t)(x) << 8)  & 0xff00000000ULL) | \
                        (((uint64_t)(x) >> 8)  & 0xff000000ULL) | \
                        (((uint64_t)(x) >> 24) & 0xff0000ULL) | \
                        (((uint64_t)(x) >> 40) & 0xff00ULL) | \
                        ((uint64_t)(x)  >> 56))

#define ntohll BSWAP_64
#define htonll ntohll

#endif
